package com.zx._12_算法.左程云算法练习;

/**
 * 随机概率
 */
public class Case_04 {

    public static void main(String[] args) {
        test5();
    }

    /**
     * 1.验证随机数的准确性
     */
    private static void test1() {
        int counter = 0;
        int total = 10000;
        for (int i = 0; i < total; i++) {
            // 数据范围 [0, 1)
            double random = Math.random();
            if (random < 0.7) {
                counter++;
            }
        }
        System.out.println((double) counter / (double) total);
    }

    /**
     * 2.3个随机值都在区间内的概率
     */
    private static void test2() {
        int counter = 0;
        int total = 10000;
        double x = 0.6;
        for (int i = 0; i < total; i++) {
            // 数据范围 [0, 1)，随机三个数字，都在[0, x)范围的概率就是 x^3
            double random = Math.max(Math.random(), Math.max(Math.random(), Math.random()));
            if (random < x) {
                counter++;
            }
        }
        // 出现的概率
        System.out.println((double) counter / (double) total);
        System.out.println(Math.pow(x, 3));
    }

    /**
     * 3.3个随机值，至少有一个在范围
     */
    private static void test3() {
        int counter = 0;
        int total = 10000;
        double x = 0.6;
        for (int i = 0; i < total; i++) {
            // 数据范围 [0, 1)，随机三个数字，都在[0, x)范围的概率就是 1-(1-x)^3
            double random = Math.min(Math.random(), Math.min(Math.random(), Math.random()));
            if (random < x) {
                counter++;
            }
        }
        // 出现的概率
        System.out.println((double) counter / (double) total);
        System.out.println(1 - Math.pow(1 - x, 3));
    }

    /**
     * 4. 游戏抽奖概率
     */
    private static void test4() {
        // 1% 1等奖
        // 4% 2等奖
        // 15% 3等奖
        // 80% 未中奖
        int j1 = 0;
        int j2 = 0;
        int j3 = 0;
        int j4 = 0;

        int total = 1000_1000;
        for (int i = 0; i < total; i++) {
            double random = Math.random() * 100;
            if (random < 1) {
                j1++;
            } else if (random >= 1 && random < 5) {
                j2++;
            } else if (random >= 5 && random < 20) {
                j3++;
            } else {
                j4++;
            }
        }
        System.out.println("1等奖：" + (double) j1 / (double) total);
        System.out.println("2等奖：" + (double) j2 / (double) total);
        System.out.println("3等奖：" + (double) j3 / (double) total);
        System.out.println("未中奖：" + (double) j4 / (double) total);
    }

    /**
     * 5. 3 ~ 5  3个数字等概率获得
     */
    private static void test5() {
        int[] res = new int[6];
        int total = 1000_0000;
        for (int i = 0; i < total; i++) {
            res[f2()]++;
        }
        for (int re : res) {
            System.out.println(re);
        }
    }

    private static int f2() {
        // 3到5等概率
        int r = (int) (Math.random() * 3) + 2;

        // 1到5等概率
        // int r = (int) (Math.random() * 5) + 1;
        return r;
    }

}
